package yxy.cherry.battle.module.affix.skill;

import yxy.cherry.battle.Battle;
import yxy.cherry.battle.Fighter;
import yxy.cherry.battle.module.affix.AffixContext;
import yxy.cherry.battle.module.affix.AffixFactory;
import yxy.cherry.battle.module.affix.Skill;
import yxy.cherry.battle.module.affix.SkillType;
import yxy.cherry.battle.module.record.stream.RecordStream44ShareDamage;
import yxy.cherry.data.bean.skill;

import java.util.ArrayList;
import java.util.Collection;

/**
 * @author James
 * 伤害均摊（所受伤害降低X%并均摊到一定数量的武将身上）
 * @date 2022/2/25 18:33
 **/
@SkillType(Type = AffixFactory.SkillTypeEnum.伤害均摊)
public class Affix25DamageShare extends Skill {
    public Affix25DamageShare(Fighter actor, skill skilldata, int probability, int[] params) {
        super(actor, skilldata, probability, params);
    }
    private int getReduceRate() {
        return getParam(0);
    }

    long shareDamage;
    AffixContext.AffixAction affixAction;

    @Override
    public boolean _exec(Collection<Fighter> targets) {
        boolean 有效 = false;
        affixAction = getAffixAction();
        AffixContext.AffixTarget affixTarget = affixAction.getTarget(getActor());
        ArrayList<Long> 结算伤害流 = genReduceDamage(affixTarget.get伤害流(), affixTarget.getBaseDamageList(), getReduceRate());
        Battle.battleLogger(6,"伤害均摊减伤：当前行动者："+affixAction.get行动者().getName()+"针对目标："+getActor().getName()+"总伤害："+affixTarget.get伤害流().toString()+"基本伤害："+affixTarget.getBaseDamageList().toString()+"减伤比例："+getReduceRate()+"减伤后："+结算伤害流.toString());
        affixTarget.set伤害流(结算伤害流);
        long 总伤害 = getAffixAction().getTarget(getActor()).总伤害();


        shareDamage = 总伤害/(targets.size()+1);

        //先修改自己的伤害数
        ArrayList<Long> 伤害流 = affixTarget.get伤害流();
        for(int i=0;i<伤害流.size();i++){
            伤害流.set(i,shareDamage/伤害流.size());
        }

        for (Fighter target : targets) {
            有效 |= _exec(getActor(), target);
        }
        return 有效;
    }

    @Override
    public boolean exec(Fighter actor, Fighter target) {
        if (!target.is活着()) {
            return false;
        }

        target.set伤害(shareDamage);
        Battle.battleLogger(6,"伤害均摊减伤：分担者："+target.getName()+"分担伤害："+shareDamage);
        getRecord().stream(new RecordStream44ShareDamage(actor,target,shareDamage)).record();

        getRecord().get统计().getFighter(affixAction.get行动者().getFighterPosition()).addDMG(shareDamage);
        getRecord().get统计().getFighter(target.getFighterPosition()).addHUT(shareDamage);

        return true;
    }
}
